import {BaseInput} from './BaseInput';
import {AjaxMsg} from '../utils/AjaxMsg';

export class UserNameInput extends BaseInput {
    constructor(form, dom){
        super(form, dom);

        this._label = '用户名';

        this.dom.on('change', this.onChange.bind(this));
    }

    onChange(){
        if(this.dom.val().length > 0){
            this.verifyUserName();
        }
    }

    verifyUserName(){
        if(this.validate()){
            this.form.onChangeUser();
            this.api.validateUserName({userName: this.getValue()}).then((msg) => {
                let ajaxMsg = new AjaxMsg(msg);
                ajaxMsg.showFocus(this.form);
                if(ajaxMsg.status === -1){
                    this.form.disableLogin('请注册账号');
                }
                this.form.shouldUseCaptcha = !!ajaxMsg.data.shouldUseCaptcha;
            });
        }
    }
}